package com.arsl.client.view;

import java.util.TreeMap;

import com.arsl.client.GlobalInfo;
import com.arsl.client.data.Content;
import com.arsl.client.util.Constants;
import com.arsl.client.util.PropertyManager;
import com.arsl.client.util.Constants.ContentStatus;
import com.arsl.client.util.Utils;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.PushButton;
import com.google.gwt.user.client.ui.StackLayoutPanel;

public class ContentAccordion extends StackLayoutPanel {
	// ArrayList<Content> contentList;
	private static final String DOWNLOAD_PAGES_FOLDER = "/dw_pages";
	TreeMap<String, ContentInputWidget> contentList;

	public ContentAccordion(Unit unit) {
		super(unit);
		contentList = new TreeMap<String, ContentInputWidget>();

	}

	public ContentAccordion() {
		this(Unit.PX);
		for (int i = 0; i < 3; i++) {

			ContentInputWidget widget = GetWidget();
			contentList.put(widget.getContentUUID(), widget);
			widget = contentList.get(widget.getContentUUID());
			/*
			 * PushButton btn = Utils.getImageButton("",
			 * removeButtonClickHandler,"", "","g-button-remove", "",
			 * widget,DOWNLOAD_PAGES_FOLDER+"/button_red_close.gif");
			 */
			Button btn = Utils.getButton("", removeButtonClickHandler, "", "",
					"", "g-button-remove", widget);
			HorizontalPanel headerPanel = new HorizontalPanel();
			HTML html = new HTML();
			html.setHTML("Enter new content");
			headerPanel.add(html);
			headerPanel.add(btn);
			headerPanel.setWidth("100%");
			this.add(widget, headerPanel, 30);
		}
	}

	public void addContent(Content[] contentToAdd) {
		try {
			for (Content content : contentToAdd) {
				ContentInputWidget widget = GetWidget(content);
				contentList.put(content.getUUID(), widget);
				addWidgetToAccordion(widget);
			}
		} catch (Exception e) {
			Window.alert(e.getMessage());
		}
	}

	public Content GetInnerContent(ContentInputWidget widget) {

		try {
			Content content = new Content();
			content.setId(widget.getContentId());
			content.setUUID(widget.getContentUUID());
			content.setStatus(widget.getContentStatus());
			content.setType(widget.getType());
			content.setTitle(widget.getTitle());
			content.setDescription(widget.getDescription());
			content.setUrl(widget.getUrl());
			content.setPublisherId(GlobalInfo.publisherId);
			return content;
		} catch (Throwable e) {
			Window.alert(e.getMessage());
			return null;
		}
	}

	public void addNewWidget() {
		try {
			ContentInputWidget widget = GetWidget();
			contentList.put(widget.getContentUUID(), widget);
			addWidgetToAccordion("New Title", widget);
		} catch (Exception e) {
			Window.alert(e.getMessage());
		}
	}

	/*
	 * Private Methods
	 */

	private void addWidgetToAccordion(ContentInputWidget widget) {
		addWidgetToAccordion(widget.getTitle(), widget);
	}
	
	private void addWidgetToAccordion(String title, ContentInputWidget widget)
	{
		Button btn = Utils.getButton("", removeButtonClickHandler,"", "","", "g-button-remove",
				widget);
		
		HorizontalPanel headerPanel = new HorizontalPanel();
		HTML html = new HTML();
		html.setHTML(title);
		headerPanel.add(html);
		headerPanel.add(btn);
		headerPanel.setWidth("100%");
		this.setHeight(String.valueOf(this.getOffsetHeight() + 35) + "px");
		this.add(widget, headerPanel, 30);
		this.showWidget(widget);
	}

	private Content getNewContent() {
		Content content = new Content();
		content.setStatus(ContentStatus.New);
		content.setId(-1);
		content.setTitle("");
		content.setUrl("");
		content.setDescription("");
		content.setType(Constants.WEB_PAGE_CONTENT_TYPE);
		content.setUUID(Utils.randomUUID());
		content.setPublisherId(GlobalInfo.publisherId);
		return content;
	}

	private ContentInputWidget GetWidget() {
		return GetWidget(getNewContent());
	}

	private ContentInputWidget GetWidget(Content content) {
		ContentInputWidget widget = new ContentInputWidget();
		widget.setContentUUID(content.getUUID());
		widget.setContentId(content.getId());
		widget.setContentStatus(content.getStatus());
		widget.setTitle(content.getTitle());
		widget.setUrl(content.getUrl());
		widget.setType(content.getType());
		widget.setDescription(content.getDescription());
		return widget;
	}

	/*
	 * Event Handlers
	 */
	ClickHandler removeButtonClickHandler = new ClickHandler() {

		@Override
		public void onClick(ClickEvent event) {
			try {
				ContentInputWidget widgetToRemove = ((ContentButton) event
						.getSource()).getParentWidget();
				if (widgetToRemove.getContentStatus().equals(ContentStatus.New)) {
					contentList.remove(widgetToRemove.getContentUUID());
				} else {
					contentList.get(widgetToRemove.getContentUUID())
							.setContentStatus(ContentStatus.Delete);
				}

				ContentAccordion.this.setHeight(String
						.valueOf(ContentAccordion.this.getOffsetHeight() - 35)
						+ "px");
				ContentAccordion.this.remove(widgetToRemove);
			} catch (Exception e) {
				Window.alert(e.getMessage());
			}

		}

	};

}
